#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#include <sys.h>
#include <string.h>

#include "map.h"
#include "msx2lib.h"

uchar limit[R];

void init_limits(void) {
    uchar c;
    for (c=0;c<R;c++) {
     limit[c] = (uchar) ((sqrt((R*R)/2.0-(c*c)/4.0)-(c)/2.0));
    }
}

uint map3D[WMAX_X*WMAX_Y];

void init_map3D(void) {
    uchar i,j;
    uint *p = map3D;
    for (j=0;j<WMAX_Y;j++)
	for (i=0;i<WMAX_X;i++){
	    *p++ = (i*3/WMAX_X) + ((j*3/WMAX_Y)<<2); // 0,1,2, 4,5,6, 8,9,10
	}
    for (j=0;j<WMAX_Y;j++)
        {
            map3D[j*WMAX_X+WMAX_X/3-1] = 11;
            map3D[j*WMAX_X+WMAX_X/3] = 12;            
            map3D[j*WMAX_X+WMAX_X*2/3-1] = 13;
            map3D[j*WMAX_X+WMAX_X*2/3] = 14;            
            
        }
    p = map3D;
    for (j=0;j<WMAX_Y;j++)
	for (i=0;i<WMAX_X;i++){
	    *p++ = *p<<1;			// pecompute (uint) offsets
	}
    
}

extern uchar sourceX,sourceY;
extern uchar wX,wY;
extern uint* shadowbuff;

///////////////////////////
void    go_up(void) {
	// uint *q = &clear_map[sourceX-wX+(sourceY-wY-1)*WMAX_X];
    // if ((*q >= Blocking) && (*(q+2) >= Blocking) )
	sourceY--;
}

void    go_rg(void) {
	// uint *q = &clear_map[sourceX+3-wX+(sourceY-wY)*WMAX_X];
    // if ((*q >= Blocking) &&(*(q+2*WMAX_X) >= Blocking) )
	sourceX++;
}

void    go_dw(void) {
	// uint *q = &clear_map[sourceX-wX+(sourceY-wY+3)*WMAX_X];
    // if ((*q >= Blocking) && (*(q+2) >= Blocking) )
	sourceY++;
}

void    go_lf(void) {
	// uint *q = &clear_map[sourceX-1-wX+(sourceY-wY)*WMAX_X];
    // if ((*q >= Blocking) &&(*(q+2*WMAX_X) >= Blocking) )
	sourceX--;
}

///////////////////////


void kilbuf(void);

uchar msxtype(void);
uchar is_pal(void);

uchar sourceX,sourceY;
uchar wX,wY;

void init_sc5_draw(void);
void sc5_draw_map(void);

char msxver;

void tst_tileset_sc5 (void);

int  game_main(void);

int  game_main() {
	uint clock1,clock2;
	uint nframe = 0;
	uchar ndir = 37;
	
	uchar key;

	scr(5);
	init_screen();
	init_sc5_draw();

    init_limits();
    init_map3D();

    sourceX = StartX*3;
	sourceY = StartY*3;
	wX = 0;
	wY = 0;

    clock1 = *((uint *)0xFC9E) ;   // start test

	while(key!=0xFF)    {
		// uint *q;

		if  ((sourceX>=WMAX_X/2-1)&&(sourceX<=MAX_X-WMAX_X/2-1))
			wX = sourceX - WMAX_X/2 + 1;
		if  ((sourceY>=WMAX_Y/2-1)&&(sourceY<=MAX_Y-WMAX_Y/2-1))
			wY = sourceY - WMAX_Y/2 + 1;

		sc5_draw_map();
		
		// q = shadowbuff + sourceX-wX+(sourceY-wY)*WMAX_X;

		// *(q+0) = view3dmap[patterns[9*ndir+0]];  
		// *(q+1) = view3dmap[patterns[9*ndir+1]];
		// *(q+2) = view3dmap[patterns[9*ndir+2]];  
		// q+=WMAX_X;
		// *(q+0) = view3dmap[patterns[9*ndir+3]];  
		// *(q+1) = view3dmap[patterns[9*ndir+4]];
		// *(q+2) = view3dmap[patterns[9*ndir+5]];  
		// q+=WMAX_X;
		// *(q+0) = view3dmap[patterns[9*ndir+6]];  
		// *(q+1) = view3dmap[patterns[9*ndir+7]];
		// *(q+2) = view3dmap[patterns[9*ndir+8]];  
		
		key = waitkey();

		switch (key)
		{
			case 2: go_rg();
			case 1: go_up();ndir=37;break;
			case 4: go_dw();
			case 3: go_rg();ndir=38;break;
			case 6: go_lf();
			case 5: go_dw();ndir=39;break;
			case 8: go_up();
			case 7: go_lf();ndir=40;break;
		}
		nframe++;
	}

    clock2 = *((uint *)0xFC9E);    // end test

    scr(0);
    kilbuf();

    printf("Msx type: %d \n",msxver);

    if (is_pal())
		printf(" PAL (50Hz)\n");
    else
		printf(" NTSC (60Hz)\n");

    if (is_pal())
		printf("Average fps %d \n", nframe/((clock2-clock1)/50));
    else
		printf("Average fps %d \n", nframe/((clock2-clock1)/60));
	
	while(waitkey()!=0xFF) {}
}



int	main(void) {
	bordercolor(0);	

	msxver = msxtype();
            
    if (msxver==0)	{
		printf("Only for msx 2 and upper \n");
		exit(0);
    }
	
	scr(5);
	init_screen();
	setpage(2);
	tst_tileset_sc5(); 
	
	do
	{
	    scr(1);
		rnd_maze();
		scr(5);
		init_screen();
		game_main();
	}
	while(1);

}



